import {
  queryListData,
  queryGetData,
  updateBasicInfoData,
  updateBalanceInfoData,
} from '../services/consumer';
import { pretreatmentRemotePageListData, pretreatmentRemoteSingleData } from '../utils/tools';

export default {
  namespace: 'consumer',

  state: {},

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(queryListData, payload);
      yield put({
        type: 'handleListData',
        payload: response,
      });
    },
    *get({ payload }, { call, put }) {
      const response = yield call(queryGetData, payload);
      yield put({
        type: 'handleGetData',
        payload: response,
      });
    },
    *updateBasicInfo({ payload }, { call, put }) {
      const response = yield call(updateBasicInfoData, payload);
      yield put({
        type: 'handleBasicInfoData',
        payload: response,
      });
    },
    *updateBalanceInfo({ payload }, { call, put }) {
      const response = yield call(updateBalanceInfoData, payload);
      yield put({
        type: 'handleBalanceInfoData',
        payload: response,
      });
    },
  },

  reducers: {
    handleListData(state, action) {
      const d = action.payload;
      const v = pretreatmentRemotePageListData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleGetData(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleBasicInfoData(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleBalanceInfoData(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
  },
};
